package com.itcam.iahinterface.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.itcam.iahclientsdk.model.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.SecureRandom;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import static com.itcam.iahinterface.utils.SignUtils.genSign;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class CamClient {

    private String accessKey;

    private String secretKey;

    public CamClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String getNameByGet(String name) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpUtil.get("", paramMap);
        System.out.println("result = " + result);
        return result;
    }

    public String getNameByPost(@RequestParam String name) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpUtil.post("", paramMap);
        System.out.println("result = " + result);
        return result;
    }

    public String getUserNameByPost(@RequestBody User user) {
        String json = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post("")
                .body(json)
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println("result = " + result);
        return result;
    }

    // 创建一个方法用户构造请求头
    private Map<String, String> getHeaderMap(String body) {
        // 创建一个新的HashMap对象
        HashMap<String, String> map = new HashMap<>();
        map.put("accessKey", accessKey);
//        map.put("secretKey", secretKey);
        map.put(body, genSign(body, secretKey));
        map.put("nonce", RandomUtil.randomNumbers(4));
        map.put("body", body);
        map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        return map;
    }

    // 生成指定长度的随机密钥
    private static String generateRandomKey(int length) {
        SecureRandom secureRandom = new SecureRandom();
        byte[] keyBytes = new byte[length];
        secureRandom.nextBytes(keyBytes);
        return Base64.getUrlEncoder().withoutPadding().encodeToString(keyBytes);
    }

    // 生成 accessKey 和 secretKey
    public static KeyPair generateKeys() {
        String accessKey = generateRandomKey(16);
        String secretKey = generateRandomKey(32);
        return new KeyPair(accessKey, secretKey);
    }

    // 密钥对类
    public static class KeyPair {
        private final String accessKey;
        private final String secretKey;

        public KeyPair(String accessKey, String secretKey) {
            this.accessKey = accessKey;
            this.secretKey = secretKey;
        }

        public String getAccessKey() {
            return accessKey;
        }

        public String getSecretKey() {
            return secretKey;
        }
    }

    // ... existing code ...

    public static void main(String[] args) {
//        KeyPair keys = generateKeys();
//        System.out.println("Access Key: " + keys.getAccessKey());
//        // 这里不要直接返回 Secret Key，仅在服务器端使用
//        System.out.println("Secret Key: 不直接返回给客户端" + keys.getSecretKey());

        String account = "cammy";
        String password = "password";
        HashMap<String, String> map = new HashMap<>();
        map.put(account, password);
        System.out.println(map.toString());
    }


}
